﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.IO
{
    /// <summary>
    /// This class provides methods to write one or more complex polynomials to a xml file.
    /// </summary>
    [Serializable]
    public class ComplexPolynomialFileWriter : AbstractXmlFileWriter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="GeneralVectorFileWriter"/> class.
        /// </summary>
        /// <param name="stream">The stream of the file writer.</param>
        /// <param name="encoding">The encoding of the file.</param>
        public ComplexPolynomialFileWriter(Stream stream, Encoding encoding)
            : base(stream, encoding)
        {
            this.writeEngine.WriteStartElement("ComplexPolynomials");
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="GeneralVectorFileWriter"/> class.
        /// </summary>
        /// <param name="filename">The filename of the file to create.</param>
        /// <param name="encoding">The encoding of the file.</param>
        public ComplexPolynomialFileWriter(String filename, Encoding encoding)
            : base(filename, encoding)
        {
            this.writeEngine.WriteStartElement("ComplexPolynomials");
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="GeneralVectorFileWriter"/> class.
        /// </summary>
        /// <param name="textWriter">The text writer of the writer.</param>
        public ComplexPolynomialFileWriter(TextWriter textWriter)
            : base(textWriter)
        {
            this.writeEngine.WriteStartElement("ComplexPolynomials");
        }

        /// <summary>
        /// Writes the specified complex polynomial to the specified file.
        /// </summary>
        /// <param name="polynomial">The complex polynomial to write.</param>
        public void Write(ComplexPolynomial polynomial)
        {
            this.writeEngine.WriteStartElement("ComplexPolynomial");

            for (int i = 0; i < polynomial.Components.Count; i++)
            {
                this.writeEngine.WriteStartElement("Element");
                this.writeEngine.WriteElementString("CoefficientRealNumberPart",
                                                    polynomial.Components[i].Coefficient.RealNumberPart.ToString());
                this.writeEngine.WriteElementString("CoefficientImaginaryNumberPart",
                                                    polynomial.Components[i].Coefficient.ImaginaryNumberPart.ToString());
                this.writeEngine.WriteElementString("Exponent", polynomial.Components[i].Exponent.ToString());
                this.writeEngine.WriteEndElement();
            }

            this.writeEngine.WriteEndElement();
        }

        /// <summary>
        /// Closes the writer and saves the output values.
        /// </summary>
        public override void Close()
        {
            this.writeEngine.WriteEndElement();
            base.Close();
        }
    }
}